package com.cdie.myschool.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.cdie.myschool.common.R;
import com.cdie.myschool.domain.entity.*;
import com.cdie.myschool.mapper.CalendarMapper;
import com.cdie.myschool.mapper.DepartmentReportMapper.DepartmentOldMapper;
import com.cdie.myschool.mapper.StudentTermReportMapper.*;
import com.cdie.myschool.service.StudentDataImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 朱华扬
 * @date 2022/10/12 19:22
 * @Description
 */
@Service
public class StudentDataImportServiceImpl implements StudentDataImportService {
    @Autowired
    private CalendarMapper calendarMapper;
    @Autowired
    private CalendarOldMapper calendarOldMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentOldMapper studentOldMapper;
    @Autowired
    private DepartmentDataMapper departmentDataMapper;
    @Autowired
    private DepartmentOldMapper departmentOldMapper;
    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private MajorOldMapper majorOldMapper;
    @Autowired
    private ClassesDataMapper classesMapper;
    @Autowired
    private ClassesOldMapper classesOldMapper;
    @Autowired
    private CourseDataMapper courseMapper;
    @Autowired
    private CourseOldMapper courseOldMapper;
    @Autowired
    private ScholarshipMapper scholarshipMapper;
    @Autowired
    private ScholarshipOldMapper scholarshipOldMapper;
    @Autowired
    private QualityEducationMapper qualityEducationMapper;
    @Autowired
    private QualityEducationOldMapper qualityEducationOldMapper;

    /**
     * 功能描述: 获取学年学期表原始数据
     * @Param: []
     * @Return: java.util.List<com.cdie.myschool.domain.entity.CalendarOld>
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:14
     */
    @DS("slave_1")
    public List<CalendarOld> getCalendarInfo(){
        //获取原始数据
        List<CalendarOld> calendarOldList = calendarOldMapper.selectList(null);
        return calendarOldList;
    }
    /**
     * 功能描述: 将学年学期表原始数据导入
     * @Param: [calendarOldList]
     * @Return: int
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:14
     */
    @Override
    @DS("master")
    public R setCalendarInfo() throws ParseException {
        List<CalendarOld> calendarOldList = getCalendarInfo();
        int res = 0;
        //循环获取学年学期信息
        for(CalendarOld calendarOld: calendarOldList) {
            //实例化calendar，用于存储数据
            Calendar calendar = new Calendar();
            //实例化日期处理格式
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //使用正则表达式处理时间格式
            String reg1 = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
            String reg2 = "$1-$2-$3 $4:$5:$6";
            //若数据值不等于空，则将值赋值给calendar对应字段
            if (calendarOld.getXN()!=null){
                calendar.setSchoolYear(calendarOld.getXN());
            }
            if (calendarOld.getXQ()!=null){
                calendar.setSemester(calendarOld.getXQ());
            }
            if(calendarOld.getKSRQ()!=null){
                calendar.setBeginTime(sdf1.parse(calendarOld.getKSRQ()));
            }
            if(calendarOld.getJSRQ()!=null){
                calendar.setEndTime(sdf1.parse(calendarOld.getJSRQ()));
            }
            if(calendarOld.getSKZS()!=null){
                calendar.setClassWeeks(calendarOld.getSKZS());
            }
            if (calendarOld.getTSTAMP()!=null){
                calendar.setTimestamp(sdf2.parse(calendarOld.getTSTAMP().replaceAll(reg1,reg2)));
            }
            //向学年学期表中插入数据
            res = calendarMapper.insert(calendar);
        }
        if (res==1){
            return R.Success("导入成功");
        }else {
            return R.Failed("导入失败");
        }
    }

    /**
     * 功能描述: 获取学生信息表原始数据
     * @Param: []
     * @Return: java.util.List<com.cdie.myschool.domain.entity.StudentOld>
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:15
     */
    @DS("slave_1")
    public List<StudentOld> getStudentInfo(){
        //获取原始数据
        List<StudentOld> studentOldList = studentOldMapper.selectList(null);
        return studentOldList;
    }
    /**
     * 功能描述: 将学生信息导入
     * @Param: [studentOldList]
     * @Return: int
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:17
     */
    @Override
    @DS("master")
    public R setStudentInfo() throws ParseException {
        List<StudentOld> studentOldList = getStudentInfo();
        int res = 0;
        //循环获取学生信息
        for(StudentOld studentOld: studentOldList) {
            //实例化student，用于存储数据
            Student student = new Student();
            //实例化日期处理格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //使用正则表达式处理时间格式
            String reg1 = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
            String reg2 = "$1-$2-$3 $4:$5:$6";
            //若数据值不等于空，则将值赋值给student对应字段
            if (studentOld.getXH()!=null){
                student.setStudentCode(studentOld.getXH());
            }
            if (studentOld.getXM()!=null){
                student.setStudentName(studentOld.getXM());
            }
            if (studentOld.getNJ()!=null){
                student.setStudentGrade(studentOld.getNJ());
            }
            if (studentOld.getBJBM()!=null){
                student.setStudentClassCode(studentOld.getBJBM());
            }
            if(studentOld.getZYBM()!=null){
                student.setStudentMajorCode(studentOld.getZYBM());
            }
            if (studentOld.getYXSH()!=null){
                student.setDepartmentCode(studentOld.getYXSH());
            }
            if (studentOld.getSFZX()!=null){
                student.setStudentIn(Byte.valueOf(studentOld.getSFZX()));
            }
            if (studentOld.getRXRQ()!=null){
                student.setAdmissionDate(sdf.parse(studentOld.getRXRQ().replaceAll(reg1,reg2)));
            }
            if (studentOld.getXJZTM()!=null){
                student.setSchoolRollStatus(Byte.valueOf(studentOld.getXJZTM()));
            }
            if (studentOld.getTSTAMP()!=null){
                student.setTimestamp(sdf.parse(studentOld.getTSTAMP().replaceAll(reg1,reg2)));
            }
            //向学生表中插入数据
            res = studentMapper.insert(student);
        }
        if (res==1){
            return R.Success("导入成功");
        }else {
            return R.Failed("导入失败");
        }
    }

    /**
     * 功能描述: 获取院系信息原始数据
     * @Param: []
     * @Return: java.util.List<com.cdie.myschool.domain.entity.DepartmentOld>
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:18
     */
    @DS("slave_1")
    public List<DepartmentOld> getDepartmentInfo(){
        //获取院系信息原始数据
        List<DepartmentOld> departmentOldList = departmentOldMapper.selectList(null);
        return departmentOldList;
    }

    /**
     * 功能描述: 将院系信息导入
     * @Param: [departmentOldList]
     * @Return: int
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:18
     */
    @Override
    @DS("master")
    public R setDepartmentInfo() throws ParseException {
        List<DepartmentOld> departmentOldList = getDepartmentInfo();
        int res = 0;
        //循环获取院系信息
        for(DepartmentOld departmentOld: departmentOldList) {
            //实例化department，用于存储数据
            DepartmentData department = new DepartmentData();
            //实例化日期处理格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //使用正则表达式处理时间格式
            String reg1 = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
            String reg2 = "$1-$2-$3 $4:$5:$6";
            //判断字段是否为空，将原始数据的值赋值给department对应字段
            if (departmentOld.getDWBM()!=null){
                department.setDepartmentCode(departmentOld.getDWBM());
            }
            if (departmentOld.getDWMC()!=null){
                department.setDepartmentName(departmentOld.getDWMC());
            }
            if (departmentOld.getDWYXBS()!=null){
                department.setDepartmentStatus(Integer.valueOf(departmentOld.getDWYXBS()));
            }
            if (departmentOld.getLSDWH()!=null){
                department.setDepartmentParentCode(Integer.valueOf(departmentOld.getLSDWH()));
            }
            if (departmentOld.getTSTAMP()!=null){
                department.setTimestamp(sdf.parse(departmentOld.getTSTAMP().replaceAll(reg1,reg2)));
            }
            //向数据库中插入数据
            res = departmentDataMapper.insert(department);
        }
        if (res==1){
            return R.Success("导入成功");
        }else {
            return R.Failed("导入失败");
        }
    }

    /**
     * 功能描述: 获取学生专业原始数据
     * @Param: []
     * @Return: java.util.List<com.cdie.myschool.domain.entity.MajorOld>
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:18
     */
    @DS("slave_1")
    public List<MajorOld> getMajorInfo(){
        //获取原始数据
        List<MajorOld> majorOldList = majorOldMapper.selectList(null);
        return majorOldList;
    }
    /**
     * 功能描述: 将学生专业信息导入
     * @Param: [majorOldList]
     * @Return: int
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:19
     */
    @Override
    @DS("master")
    public R setMajorInfo() throws ParseException {
        List<MajorOld> majorOldList = getMajorInfo();
        int res = 0;
        //循环获取专业信息
        for(MajorOld majorOld: majorOldList) {
            //实例化major，用于存储数据
            Major major = new Major();
            //实例化日期处理格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //使用正则表达式处理时间格式
            String reg1 = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
            String reg2 = "$1-$2-$3 $4:$5:$6";
            //若数据值不等于空，则将值赋值给major对应字段
            if (majorOld.getZYBM()!=null){
                major.setMajorCode(majorOld.getZYBM());
            }
            if (majorOld.getZYMC()!=null){
                major.setMajorName(majorOld.getZYMC());
            }
            if(majorOld.getXZ()!=null){
                major.setEducationalSystem(majorOld.getXZ());
            }
            if(majorOld.getYXSH()!=null){
                major.setDepartmentCode(majorOld.getYXSH());
            }
            if(majorOld.getSFQY()!=null){
                major.setMajorStatus(Byte.valueOf(majorOld.getSFQY()));
            }
            if (majorOld.getTSTAMP()!=null){
                major.setTimestamp(sdf.parse(majorOld.getTSTAMP().replaceAll(reg1,reg2)));
            }
            //向专业表中插入数据
            res = majorMapper.insert(major);
        }
        if (res==1){
            return R.Success("导入成功");
        }else {
            return R.Failed("导入失败");
        }
    }

    /**
     * 功能描述: 获取班级信息原始数据
     * @Param: []
     * @Return: java.util.List<com.cdie.myschool.domain.entity.ClassesOld>
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:19
     */
    @DS("slave_1")
    public List<ClassesOld> getClassesInfo(){
        //获取原始数据
        List<ClassesOld> classesOldList = classesOldMapper.selectList(null);
        return classesOldList;
    }
    /**
     * 功能描述: 将班级信息导入
     * @Param: [classesOldList]
     * @Return: int
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:19
     */
    @Override
    @DS("master")
    public R setClassesInfo() throws ParseException {
        List<ClassesOld> classesOldList = getClassesInfo();
        int res = 0;
        //循环获取班级信息
        for(ClassesOld classesOld: classesOldList) {
            //实例化classes，用于存储数据
            ClassesData classes = new ClassesData();
            //实例化日期处理格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //使用正则表达式处理时间格式
            String reg1 = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
            String reg2 = "$1-$2-$3 $4:$5:$6";
            //若数据值不等于空，则将值赋值给classes对应字段
            if (classesOld.getBJBM()!=null){
                classes.setClassCode(classesOld.getBJBM());
            }
            if (classesOld.getBJMC()!=null){
                classes.setClassName(classesOld.getBJMC());
            }
            if(classesOld.getYXSH()!=null){
                classes.setDepartmentCode(classesOld.getYXSH());
            }
            if(classesOld.getZYBM()!=null){
                classes.setMajorCode(classesOld.getZYBM());
            }
            if(classesOld.getXQH()!=null){
                classes.setCampusAreaCode(classesOld.getXQH());
            }
            if(classesOld.getSFKY()!=null){
                classes.setClassStatus(Byte.valueOf(classesOld.getSFKY()));
            }
            if (classesOld.getTSTAMP()!=null){
                classes.setTimestamp(sdf.parse(classesOld.getTSTAMP().replaceAll(reg1,reg2)));
            }
            //向班级表中插入数据
            res = classesMapper.insert(classes);
        }
        if (res==1){
            return R.Success("导入成功");
        }else {
            return R.Failed("导入失败");
        }
    }

    /**
     * 功能描述: 获取课程信息原始数据
     * @Param: []
     * @Return: java.util.List<com.cdie.myschool.domain.entity.CourseOld>
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:20
     */
    @DS("slave_1")
    public List<CourseOld> getCourseInfo(){
        //获取原始数据
        List<CourseOld> courseOldList = courseOldMapper.selectList(null);
        return courseOldList;
    }

    /**
     * 功能描述: 将课程信息导入
     * @Param: [courseOldList]
     * @Return: int
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:20
     */
    @Override
    @DS("master")
    public R setCourseInfo() throws ParseException {
        List<CourseOld> courseOldList = getCourseInfo();
        int res = 0;
        //循环获取课程信息
        for(CourseOld courseOld: courseOldList) {
            float jsxs = 0;
            float syxs = 0;
            float sjxs = 0;
            float qtxs = 0;
            //实例化course，用于存储数据
            CourseData course = new CourseData();
            //实例化日期处理格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //使用正则表达式处理时间格式
            String reg1 = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
            String reg2 = "$1-$2-$3 $4:$5:$6";
            //若数据值不等于空，则将值赋值给course对应字段
            if (courseOld.getKCBM()!=null){
                course.setCourseCode(courseOld.getKCBM());
            }
            if (courseOld.getKCMC()!=null){
                course.setCourseName(courseOld.getKCMC());
            }
            if (courseOld.getJSXS()!=null){
                jsxs = Integer.parseInt(courseOld.getJSXS());
            }
            if (courseOld.getJSXS()!=null){
                syxs = Integer.parseInt(courseOld.getSYXS());
            }
            if (courseOld.getJSXS()!=null){
                sjxs = Integer.parseInt(courseOld.getSJXS());
            }
            if (courseOld.getJSXS()!=null){
                qtxs = Integer.parseInt(courseOld.getQTXS());
            }
            //课程学时等于讲授学时+实验学时+实践学时+其他学时
            float hours =jsxs+syxs+sjxs+qtxs;
            course.setCourseHours(String.valueOf(hours));
            if (courseOld.getYXSH()!=null){
                course.setDepartmentCode(courseOld.getYXSH());
            }
            if (courseOld.getXF()!=null){
                course.setCourseCredit(courseOld.getXF());
            }
            if (courseOld.getSFKY()!=null){
                course.setCourseStatus(Byte.valueOf(courseOld.getSFKY()));
            }
            if (courseOld.getTSTAMP()!=null){
                course.setTimestamp(sdf.parse(courseOld.getTSTAMP().replaceAll(reg1,reg2)));
            }
            //向课程表中插入数据
            res = courseMapper.insert(course);
        }
        if (res==1){
            return R.Success("导入成功");
        }else {
            return R.Failed("导入失败");
        }
    }

    /**
     * 功能描述: 获取讲学金信息原始数据
     * @Param: []
     * @Return: java.util.List<com.cdie.myschool.domain.entity.ScholarshipOld>
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:21
     */
    @DS("slave_1")
    public List<ScholarshipOld> getScholarship(){
        //获取学生奖学金信息原始数据
        List<ScholarshipOld> scholarshipOldList = scholarshipOldMapper.selectList(null);
        return scholarshipOldList;
    }

    /**
     * 功能描述: 将奖学金信息导入
     * @Param: [scholarshipOldList]
     * @Return: int
     * @Author: 朱华扬
     * @Date: 2022/10/13 11:21
     */
    @Override
    @DS("master")
    public R setScholarship() throws ParseException {
        List<ScholarshipOld> scholarshipOldList = getScholarship();
        int res = 0;
        //循环获取学生奖学金信息
        for(ScholarshipOld scholarshipOld: scholarshipOldList) {
            //实例化scholarship，用于存储数据
            Scholarship scholarship = new Scholarship();
            //实例化日期处理格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //使用正则表达式处理时间格式
            String reg1 = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
            String reg2 = "$1-$2-$3 $4:$5:$6";
            //判断字段是否为空，将原始数据的值赋值给scholarship对应字段
            /*if (StudentScholarshipInfo.getXNXQ()!=null){

            }*/
            if (scholarshipOld.getXH()!=null){
                scholarship.setStudentCode(scholarshipOld.getXH());
            }
            if (scholarshipOld.getJXJMC()!=null){
                scholarship.setScholarshipName(scholarshipOld.getJXJMC());
            }
            if (scholarshipOld.getTSTAMP()!=null){
                scholarship.setTimestamp(sdf.parse(scholarshipOld.getTSTAMP().replaceAll(reg1,reg2)));
            }
            //向数据库中插入数据
            res = scholarshipMapper.insert(scholarship);
        }
        if (res==1){
            return R.Success("导入成功");
        }else {
            return R.Failed("导入失败");
        }
    }

    /**
     * 功能描述:获取学生素质教育信息原始数据
     * @Param: []
     * @Return: java.util.List<com.cdie.myschool.domain.entity.QualityEducationOld>
     * @Author: 朱华扬
     * @Date: 2022/10/13 9:41
     */
    @DS("slave_1")
    public List<QualityEducationOld> getQualityEducation(){
        //获取原始数据
        List<QualityEducationOld> qualityEducationOldList = qualityEducationOldMapper.selectList(null);
        return qualityEducationOldList;
    }
    /**
     * 功能描述:将学生素质教育信息导入
     * @Param: [qualityEducationOldList]
     * @Return: int
     * @Author: 朱华扬
     * @Date: 2022/10/13 9:41
     */
    @Override
    @DS("master")
    public R setQualityEducation() throws ParseException {
        List<QualityEducationOld> qualityEducationOldList = getQualityEducation();
        int res = 0;
        //循环获取素质教育积分信息
        for(QualityEducationOld qualityEducationOld: qualityEducationOldList) {
            //实例化qualityEducation，用于存储数据
            QualityEducation qualityEducation = new QualityEducation();
            //实例化日期处理格式
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //使用正则表达式处理时间格式
            String reg1 = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
            String reg2 = "$1-$2-$3 $4:$5:$6";
            //若数据值不等于空，则将值赋值给qualityEducation对应字段
            if (qualityEducationOld.getXH()!=null){
                qualityEducation.setStudentCode(qualityEducationOld.getXH());
            }
            if (qualityEducationOld.getHDXN()!=null&&qualityEducationOld.getHDXQ()!=null){
                String [] strings = qualityEducationOld.getHDXN().split("-");
                String activityDay="";
                if (qualityEducationOld.getHDXQ().equals("01")){
                    activityDay="0";
                }
                if (qualityEducationOld.getHDXQ().equals("02")){
                    activityDay="1";
                }
                if (activityDay!=""){
                    //创建查询条件构造器
                    LambdaQueryWrapper<Calendar> XNXQ = new LambdaQueryWrapper<>();
                    Map<SFunction<Calendar,?>, Object> map = new HashMap<>();
                    map.put(Calendar::getSchoolYear,strings[0].replaceAll(" ", ""));
                    map.put(Calendar::getSemester,activityDay);
                    //根据学年，学期获取学年学期id
                    int semesterId = calendarMapper.selectOne(XNXQ.allEq(map)).getSemesterId();
                    qualityEducation.setSemesterId(semesterId);
                }
            }
            if(qualityEducationOld.getHDMC()!=null){
                qualityEducation.setActivityName(qualityEducationOld.getHDMC());
            }
            if(qualityEducationOld.getHDLX()!=null){
                qualityEducation.setActivityType(qualityEducationOld.getHDLX());
            }
            if (qualityEducationOld.getJF()!=null){
                qualityEducation.setScore(Integer.parseInt(qualityEducationOld.getJF()));
            }
            if (qualityEducationOld.getTSTAMP()!=null){
                qualityEducation.setTimestamp(sdf2.parse(qualityEducationOld.getTSTAMP().replaceAll(reg1,reg2)));
            }
            //向素质教育积分表中插入数据
            res = qualityEducationMapper.insert(qualityEducation);
        }
        if (res==1){
            return R.Success("导入成功");
        }else {
            return R.Failed("导入失败");
        }
    }
}
